﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmPhieuNhapKho : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        private PhieuNhapChiTietBOCollection PhieuNhapChiTietBOCollection = PhieuNhapChiTietBOCollection.NewPhieuNhapChiTietBOCollection();

        private PhieuNhapBO PhieuNhapBO = PhieuNhapBO.NewRootPhieuNhapBO();
        private PhieuNhapBOCollection PhieuNhapBOCollection = PhieuNhapBOCollection.NewPhieuNhapBOCollection();

        #endregion variant

        //=====================================
        #region form
        public FrmPhieuNhapKho()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmPhieuNhapKho_Load);
            this.FormClosing += new FormClosingEventHandler(FrmPhieuNhapKho_FormClosing);

            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);

            this.gridViewPhieuNhap.RowCellClick += new RowCellClickEventHandler(this.gridViewPhieuNhap_RowCellClick);


            this.grdViewPhieuNhapChiTiet.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmPhieuNhapKho_Load(object sender, EventArgs e)
        {
            //SetFormStatus(this.formStatus);

            Utility.InitControl(this.reLookUpMatHang);
            Utility.InitControl(this.reLookUpDonViTinh);
            Utility.InitControl(this.reLookUpDMKhoID);
            Utility.InitControl(this.lookUpEditNhaCungCap);
            Utility.InitControl(this.reItemLookUpNhaCungCap);

            Utility.InitControl(this.lookUpNguoiLapPhieuID);
            Utility.InitControl(this.reLookUpNguoiLapID);

            RefreshData();

        }

        private void FrmPhieuNhapKho_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }

        #endregion form

        //=====================================
        #region control

        private void spinEditTienPhatSinh_ValueChanged(object sender, EventArgs e)
        {
            TinhTien();
        }
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnInvaLuu_Click(object sender, EventArgs e)
        {
            Save();

            if (StaticUtil.GetRealInt(PhieuNhapBO.PhieuNhapID.Value, -1) >= 0)
            {
                RPhieuNhapBO rPhieuNhapBO = RPhieuNhapBO.NewRootRPhieuNhapBO();
                rPhieuNhapBO.CopyFromRPhieuNhapBO(PhieuNhapBO);
                Report.rpPhieuNhapKho rPhieuNhapKho = new TanHungLong.Report.rpPhieuNhapKho();
                rPhieuNhapKho.SetReportData(rPhieuNhapBO, PhieuNhapChiTietBOCollection);
                rPhieuNhapKho.ShowPreviewDialog();
            }
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion button

        //=====================================
        #region grid

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void gridViewPhieuNhap_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            int PhieuNhapID = StaticUtil.GetInt(gridViewPhieuNhap.GetFocusedRowCellValue("PhieuNhapID"));
            this.PhieuNhapBO = Model.PhieuNhapBO.GetRootPhieuNhapBO(PhieuNhapID);
            this.PhieuNhapChiTietBOCollection = Model.PhieuNhapChiTietBOCollection.GetPhieuNhapChiTietBOCollection("PhieuNhapID = " + PhieuNhapID.ToString());

            lookUpEditNhaCungCap.EditValue = StaticUtil.GetInt(PhieuNhapBO.NhaCungCapID.Value, -1);
            lookUpNguoiLapPhieuID.EditValue = StaticUtil.GetInt(PhieuNhapBO.USRC.Value, -1);

            txtMaSo.Text = PhieuNhapBO.MaSo;
            txtNoidung.Text = PhieuNhapBO.NoiDung;
            txtGhichu.Text = PhieuNhapBO.GhiChu;
            dtNgayLapHoaDon.DateTime = PhieuNhapBO.NgayLapHoaDon.Value;

            spinEditVAT.Value = (decimal)PhieuNhapBO.MucVAT;
            spinEditChietKhau.Value = (decimal)PhieuNhapBO.MucChietKhau;
            spinEditPhucVu.Value = (decimal)PhieuNhapBO.MucPhucVu;
            spinEditGiamGia.Value = (decimal)PhieuNhapBO.MucGiamGia;
            spinEditTienPhatSinh.Value = (decimal)PhieuNhapBO.TienPhatSinh;
            txtLyDoPhatSinh.Text = PhieuNhapBO.LyDoPhatSinh;

            bdSourcePhieuNhapChiTiet.DataSource = PhieuNhapChiTietBOCollection.Clone();

            sapxep();
            TinhTien();
        }

        #endregion grid

        //=====================================
        #region function

        private void RefreshData()
        {
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            DanhMucBOList dmKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.reLookUpDMKhoID, dmKhoBOList, dmKhoBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reLookUpMatHang, matHangBOList, matHangBOList.Count);

            NhanVienBOList nhanVienBOList = NhanVienBOList.GetNhanVienBOList();
            Utility.SetData(this.lookUpNguoiLapPhieuID, nhanVienBOList, nhanVienBOList.Count);
            Utility.SetData(this.reLookUpNguoiLapID, nhanVienBOList, nhanVienBOList.Count);

            KhachHangBOList khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.reItemLookUpNhaCungCap, khachHangBOList, khachHangBOList.Count);
            Utility.SetData(this.lookUpEditNhaCungCap, khachHangBOList, khachHangBOList.Count);

            #endregion BOList

            this.PhieuNhapBOCollection = Model.PhieuNhapBOCollection.GetPhieuNhapBOCollection("ThanhToan = 1 and TinhTrangXuatKho = 0");
            this.bdSourcePhieuNhap.DataSource = this.PhieuNhapBOCollection.Clone();

        }

        private void Save()
        {
            try
            {
                if (kiemtraLuu() == 1)
                {
                    this.PhieuNhapBO.NgayLapHoaDon = dtNgayLapHoaDon.DateTime;
                    this.PhieuNhapBO.USRC = StaticUtil.GetInt(lookUpNguoiLapPhieuID.EditValue, -1);
                    this.PhieuNhapBO.TinhTrangXuatKho = 1;

                    this.PhieuNhapBO.Save();

                    int rowCount = PhieuNhapChiTietBOCollection.Count;

                    for (int i = 0; i < rowCount; i++)
                    {
                        TonKhoBO tonKhoBO = null;
                        int matHangID = StaticUtil.GetInt(PhieuNhapChiTietBOCollection[i].MatHangID, -1);
                        int donViTinhID = StaticUtil.GetInt(PhieuNhapChiTietBOCollection[i].DonViTinhID, -1);
                        int danhMucKhoID = StaticUtil.GetInt(PhieuNhapChiTietBOCollection[i].DanhMucKhoID, -1);
                        tonKhoBO = TonKhoBO.GetRootTonKhoBOByWhere("(MatHangID = " + matHangID.ToString()
                            + ") and (DonViTinhID = " + donViTinhID.ToString()
                            + ") and (DanhMucKhoID = " + danhMucKhoID.ToString() + ")");
                        if (StaticUtil.GetInt(tonKhoBO.TonKhoID.Value, -1) < 0) tonKhoBO = TonKhoBO.NewRootTonKhoBO();
                        tonKhoBO.MatHangID = matHangID;
                        tonKhoBO.DonViTinhID = donViTinhID;
                        tonKhoBO.DanhMucKhoID = danhMucKhoID;
                        tonKhoBO.SoLuongTon = StaticUtil.GetDouble(tonKhoBO.SoLuongTon, 0) + StaticUtil.GetDouble(PhieuNhapChiTietBOCollection[i].SoLuong, 0);
                        tonKhoBO.Save();
                        MaVachBO maVachBO = null;
                        string strWhere = "TonKhoID = " + tonKhoBO.TonKhoID.ToString() + " and MaVach = '" + PhieuNhapChiTietBOCollection[i].MaVach.Trim() + "'";
                        maVachBO = MaVachBO.GetRootMaVachBOByWhere(strWhere);
                        if (StaticUtil.GetInt(maVachBO.MaVachID.Value, -1) < 0) maVachBO = MaVachBO.NewRootMaVachBO();
                        maVachBO.TonKhoID = tonKhoBO.TonKhoID;
                        maVachBO.MaVach = PhieuNhapChiTietBOCollection[i].MaVach;
                        maVachBO.SoLuongTon = StaticUtil.GetDouble(maVachBO.SoLuongTon, 0) + StaticUtil.GetDouble(PhieuNhapChiTietBOCollection[i].SoLuong, 0);
                        maVachBO.Save();
                    }

                    MessageBox.Show("Nhập kho thành công Phiếu nhập này");

                    this.PhieuNhapBOCollection = Model.PhieuNhapBOCollection.GetPhieuNhapBOCollection("ThanhToan = 1 and TinhTrangXuatKho = 0");
                    this.bdSourcePhieuNhap.DataSource = this.PhieuNhapBOCollection.Clone();
                }

            }
            catch
            {

            }
        }

        private void sapxep()
        {

            int length = PhieuNhapChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                PhieuNhapChiTietBOCollection[i].STT0 = (i + 1);
            }

            bdSourcePhieuNhapChiTiet.DataSource = PhieuNhapChiTietBOCollection;
        }

        private void TinhTien()
        {
            double tongso = 0;
            double thanhtien = 0;
            double tongtien = 0;

            int length = PhieuNhapChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                tongso = tongso + PhieuNhapChiTietBOCollection[i].SoLuong.Value;
                thanhtien = thanhtien + PhieuNhapChiTietBOCollection[i].ThanhTien.Value;
            }

            spinEditTongSo.Value = (decimal)tongso;
            spinEditThanhTien.Value = (decimal)thanhtien;

            double mucVAT = StaticUtil.GetDouble(spinEditVAT.Value, 0);
            double tienVAT = thanhtien * mucVAT;
            spinEditTienVAT.Value = (decimal)tienVAT;
            double mucCK = StaticUtil.GetDouble(spinEditChietKhau.Value, 0);
            double tienCK = thanhtien * mucCK;
            spinEditTienChietKhau.Value = (decimal)tienCK;
            double mucGG = StaticUtil.GetDouble(spinEditGiamGia.Value, 0);
            double tienGG = thanhtien * mucGG;
            spinEditTienGiamGia.Value = (decimal)tienGG;
            double mucPV = StaticUtil.GetDouble(spinEditPhucVu.Value, 0);
            double tienPV = thanhtien * mucPV;
            spinEditTienPhucVu.Value = (decimal)tienPV;
            double TienPhatSinh = StaticUtil.GetDouble(spinEditTienPhatSinh.Value, 0);

            tongtien = thanhtien + tienVAT + TienPhatSinh - tienCK - tienGG + tienPV;
            spinEditTongTien.Value = (decimal)tongtien;
            txtBangChu.Text = VNCurrency.ToString(tongtien);
        }

        private int kiemtraLuu()
        {
            int tempkiemtra = 1;
            string strError = "";

            if (lookUpEditNhaCungCap.EditValue == null)
            {
                tempkiemtra = 0;
                strError = "Chưa chọn Khách hàng";
            }

            if (txtMaSo.Text.Trim() == "")
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa nhập Mã số";
            }

            if (tempkiemtra == 0)
                MessageBox.Show(strError, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return tempkiemtra;
        }

        #endregion function

    }
}